package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkOrderPhotographyDO;
import com.golden.procedure.query.SchedulingQueryDO;
import com.golden.procedure.query.WorkPhotographyQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.IWorkPhotographyService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 摄控记录、拍摄相关接口 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.controller <br>
 *       <b>ClassName:</b> WorkPhotographyController <br>
 *       <b>Date:</b> 2018年10月23日 上午10:20:16
 */
@RestController
@RequestMapping(value = "/api/photography")
public class WorkPhotographyController extends BaseController {

	@Autowired
	IWorkPhotographyService workPhotographyService;

	public static String encodePage(PageResponse<WorkOrderPhotographyDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeComment(SingleResponse<WorkCommentDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "摄控记录列表", notes = "摄控记录列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "上门组id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "开始日期（时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "结束日期（时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "queryKey", value = "查询字段Key", required = false, dataType = "String"),
			@ApiImplicitParam(name = "queryValue", value = "查询字段值Value", required = false, dataType = "String") })
	@RequestMapping(value = "/queryScheduling.json", method = RequestMethod.POST)
	public String querySchuduling(@RequestBody QueryRequest<SchedulingQueryDO> request) {

		// {"data":
		// {"siteID":1,"pageRows":10,"schedulingDateStart":"1535817600000","schedulingDateEnd":"1535990400000"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SchedulingQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}

		PageResponse<WorkOrderPhotographyDO> response = new PageResponse<WorkOrderPhotographyDO>();
		ResultDO resultDO = workPhotographyService.querySchuduling(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkOrderPhotographyDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "拍摄列表", notes = "拍摄列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "option", value = "0-员工；1-部门；2-全部", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStr", value = "日期（时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "status", value = "拍摄状态（0-所有; 1-不看拍摄OK）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nameOrTel", value = "客户姓名或电话", required = true, dataType = "String") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<WorkPhotographyQueryDO> request) {

		// 拍摄类表
		// {"data":{"siteID":1,"employeeID":9,"pageRows":10,"schedulingDateStr":"1535817600000","status":0},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		WorkPhotographyQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		// 已登录用户信息
		if (query.getOption() == 0) {
			query.setEmployeeID(11);
		} else if (query.getOption() == 1) {
			query.setOrgID(1);
		}

		PageResponse<WorkOrderPhotographyDO> response = new PageResponse<WorkOrderPhotographyDO>();
		ResultDO resultDO = workPhotographyService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkOrderPhotographyDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			response.setCount((int) resultDO.getModel(ResultDO.THIRD_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "记录拍摄结果", notes = "记录拍摄结果")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "上拍摄记录id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "remark", value = "备注", required = true, dataType = "Integer")})
	@RequestMapping(value = "/record.json", method = RequestMethod.POST)
	public String record(@RequestBody QueryRequest<WorkCommentDO> request) {

		WorkCommentDO commentDO = request.getData();

		commentDO.setAuthorID(9);
		commentDO.setAuthor("test");

		SingleResponse<WorkCommentDO> response = new SingleResponse<WorkCommentDO>();
		ResultDO resultDO = workPhotographyService.record(commentDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeComment(response, PLANINTEXT);
	}

}
